/**
 * FileBundle.java
 */
package resources;

import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Vector;

/**
 * @author walber
 * 
 */
public class UrlBundle extends ResourceBundle {
	private static Map<String, URL> map = new HashMap<String, URL>();
	private static final ResourceBundle instance = ResourceBundle
			.getBundle("resources.UrlBundle");

	public static URL get(String key) {
		try {
			return (URL) instance.getObject(key);
		} catch (MissingResourceException ex) {
			System.out.println(String.format(
					"An exception occurred while trying to load resource %s.",
					key));
			return null;
		}
	}

	@Override
	public Enumeration<String> getKeys() {
		return (new Vector<String>(map.keySet())).elements();
	}

	@Override
	protected final Object handleGetObject(String key) {
		return this.loadUrl(key, ".txt");
	}

	private URL loadUrl(String filename, String extension) {
		String levelName = filename + extension;

		URL url = map.get(levelName);

		if (url != null)
			return url;

		url = this.getClass().getResource("levels/" + levelName);
		
		if (url == null)
			return null;

		map.put(levelName, url);

		return url;
	}

}
